"""
BC calculation
"""
import sys, os
SIZE = 5
connects = {}
f = open("connect.txt", 'r')
for s in f:
    fromto = s.split('\n')[0].split(',')
    try:
        connects[fromto[0]].extend(fromto[1])
    except KeyError:
        connects[fromto[0]] = [fromto[1]]
print connects

matrix = {}
times = 0
for uid in connects:
    matrix[uid]={uid:0}
    starts = [uid]
    for distance in range(1, SIZE):
        nextstarts = []
        for start in starts:
            try:
                for dest in connects[start]:
                    times += 1
                    try:
                        old = matrix[uid][dest]
                    except KeyError:
                        matrix[uid][dest] = distance
                        nextstarts.extend(dest)
            except KeyError:
                pass
        if nextstarts == []:
            break
        else:
            starts = nextstarts
print matrix

bc = {}
for k in matrix:
    bc[k] = 0;
    for start in matrix:
        for dest in matrix[start]:
            if start != dest:
                if k == start or k == dest:
                    bc[k] += 1
                else:
                    bc[k] += (matrix[start][k] + matrix[k][dest])/matrix[start][dest]
print bc
print "size: %d, cal times: %d" % (SIZE, times)
